using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Menu;
using Sedna.Core.Globalization;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	public class TopMenu: WebControl, IContent, IConfigurationAware
	{
		private IMenu menu;
		private string configurationId = "configuration.Menu";//default name in a Spring Context
		private string activeMenuId = HttpContext.Current.Request.QueryString["menu_level_1"];

		private String contentPlaceHolderId;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		private string ActiveMenuId
		{
			get {return activeMenuId;}
			set {activeMenuId=value;}
		}

		public IConfiguration Configuration
		{
			get { return menu; }
			set { menu = (IMenu)value; }
		}

		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		private HtmlInputHidden hiddenActiveMenu;


		public TopMenu()
		{
			menu = (IMenu)SpringContextFactory.GetContext().GetObject(configurationId);
//			EnableViewState=true;
		}


		protected override void OnInit(EventArgs e)
		{
			hiddenActiveMenu = new HtmlInputHidden();
			hiddenActiveMenu.ID = "new_menu_level_1";
//			hiddenActiveMenu.EnableViewState = false;
//			hiddenActiveMenu.ServerChange += new EventHandler(OnHiddenChange);
//			SetUpHiddenActiveMenu();
			Build(this);
			base.OnInit (e);
		}

//		protected override void LoadViewState(object savedState)
//		{
//			base.LoadViewState(savedState);
//			if (!Page.IsPostBack)
//			{
//				ActiveMenuId=Context.Request.QueryString["menu_level_1"];
//			}
//		}


		protected override void OnPreRender(EventArgs e)
		{
			ActiveteMenuItem();
			base.OnPreRender (e);
		}


		protected void Build(Control parent)
		{
			parent.Controls.Add(new System.Web.UI.LiteralControl("<td width=\"10px\" style=\"border-bottom: 1px solid gray;\">&nbsp;</td>"));
			parent.Controls.Add(hiddenActiveMenu);

			foreach(IMenuItem menuItem in menu.MenuItems)
			{
				if(!menuItem.IsVisibleForPrincipal())
				{
					continue;
				}
				TableCell tdTabLeft = new TableCell();
				tdTabLeft.ID = "tdTabLeft_" + menuItem.Id;
				tdTabLeft.EnableViewState = false;
				parent.Controls.Add(tdTabLeft);
				tdTabLeft.Width = Unit.Pixel(12);
				tdTabLeft.CssClass = "tab";
				System.Web.UI.WebControls.Image imgLeft = new System.Web.UI.WebControls.Image();
				imgLeft.ID = "tdImgLeft_" + menuItem.Id;
				imgLeft.EnableViewState = false;
				tdTabLeft.Controls.Add(imgLeft);
				imgLeft.ImageAlign = ImageAlign.AbsMiddle;
				imgLeft.ImageUrl = ImageManager.Relate(imgLeft, "unseltableft.gif");
				TableCell tdTab = new TableCell();
				tdTab.ID = "tdTab_" + menuItem.Id;
				tdTab.EnableViewState = false;
				parent.Controls.Add(tdTab);
				tdTab.Width = Unit.Percentage(1);
				tdTab.CssClass = "tab";
				tdTab.Attributes.Add("background", ImageManager.Relate(tdTab, "unseltabbg.gif"));
				tdTab.Attributes.Add("nowrap", "true");
				System.Web.UI.WebControls.LinkButton linkButton = new System.Web.UI.WebControls.LinkButton();
				linkButton.Enabled = menuItem.IsEnabledForPrincipal();
				linkButton.ID = menuItem.Id;
				tdTab.Controls.Add(linkButton);
				linkButton.Text = menuItem.GetLocalizedLabel();
				linkButton.Click += new EventHandler(OnMenuClicked);
//				linkButton.CssClass = "tab";
				linkButton.Style.Add("text-decoration", "none");
				linkButton.Attributes.Add("onclick", String.Format("document.getElementById('{0}').value='{1}';", hiddenActiveMenu.ClientID, menuItem.Id));
				linkButton.CausesValidation = menuItem.CausesValidation;
				TableCell tdTabRight = new TableCell();
				tdTabRight.ID = "tdTabRight_" + menuItem.Id;
				tdTabRight.EnableViewState = false;
				parent.Controls.Add(tdTabRight);
				tdTabRight.Width = Unit.Pixel(12);
				tdTabRight.CssClass = "tab";
				System.Web.UI.WebControls.Image imgRight = new System.Web.UI.WebControls.Image();
				imgRight.ID = "tdImgRight_" + menuItem.Id;
				imgRight.EnableViewState = false;
				tdTabRight.Controls.Add(imgRight);
				imgRight.ImageAlign = ImageAlign.AbsMiddle;
				imgRight.ImageUrl = ImageManager.Relate(imgRight, "unseltabright.gif");

			}
		}

//		private void SetUpHiddenActiveMenu()
//		{
//			string activeMenu = ExtractMenuLevel1Param();
//			if(activeMenu != null && string.Empty != activeMenu)
//			{
//				hiddenActiveMenu.Value = activeMenu;
//			}
//			else
//			{
//				//using first as a default one (TBD: implement default menu)
//				//hiddenActiveMenu.Value = ((IMenuItem)menu.MenuItems[0]).Id;
//				hiddenActiveMenu.Value = "none";
//			}
//		}

		private void OnMenuClicked(object sender, System.EventArgs e)
		{
			ActiveMenuId= ((Control)sender).ID;
			IMenuItem menuItem = menu.GetMenuItemById(ActiveMenuId);

			IActionExecutor actionExecutor = (IActionExecutor)SpringContextFactory.GetContext().GetObject("runtime.ActionExecutor");
			IExecutionContext executionContext = ExecutionContext.GetCurrent(ClientID);
			executionContext["menu_level_1"] = menuItem.Id;
			//executionContext["menu_level_2"] = "";
			actionExecutor.ExecuteAction(executionContext, menuItem, this.ClientID);
			
		}

		private void ActiveteMenuItem()
		{
			IMenuItem menuItem = menu.GetMenuItemById(ActiveMenuId);
			if(menuItem == null)
			{
				return;
			}
			
		    TableCell tdTab = (TableCell)FindControl("tdTab_" + menuItem.Id);
            if (tdTab == null)
                return;
		    tdTab.CssClass = "tabselected";
			tdTab.Attributes.Add("background", ImageManager.Relate(tdTab, "seltabbg.gif"));
			TableCell tdTabLeft = (TableCell)FindControl("tdTabLeft_" + menuItem.Id);
			tdTabLeft.CssClass = "tabselected";
			System.Web.UI.WebControls.Image tdImgLeft = (System.Web.UI.WebControls.Image)FindControl("tdImgLeft_" + menuItem.Id);
			tdImgLeft.ImageUrl = ImageManager.Relate(tdImgLeft, "seltableft.gif");
			TableCell tdTabRight = (TableCell)FindControl("tdTabRight_" + menuItem.Id);
			tdTabRight.CssClass = "tabselected";
			System.Web.UI.WebControls.Image tdImgRight = (System.Web.UI.WebControls.Image)FindControl("tdImgRight_" + menuItem.Id);
			tdImgRight.ImageUrl = ImageManager.Relate(tdImgRight, "seltabright.gif");
			System.Web.UI.WebControls.LinkButton linkButton = (System.Web.UI.WebControls.LinkButton)FindControl(menuItem.Id);
			linkButton.CssClass = "tabselected";

			Context.Session["infrastructure.menu_level_1"] = menuItem.Id;

		}

//		private void OnHiddenChange(object sender, System.EventArgs e)
//		{
//			SetUpHiddenActiveMenu();
//		}


//		private string ExtractMenuLevel1Param()
//		{
//			string menu_level_1 = Context.Request.Params["menu_level_1"];
//			if(menu_level_1 == null || menu_level_1 == string.Empty)
//			{
//				return menu_level_1;
//			}
//			if(menu_level_1.IndexOf(',', 0) < 0)
//			{
//				return menu_level_1;
//			}
//			return menu_level_1.Substring(menu_level_1.LastIndexOf(',') + 1);
//
//		}


	}
}
